package com.small.account.controller;

import com.common.basis.result.Result;
import com.common.basis.result.ResultSet;
import com.common.basis.util.VerifyCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * 验证吗
 * @author wangguochao
 */
@RestController
@RequestMapping("/login")
public class VerifyCodeController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 获取验证吗
     * @param key 随机数
     * @param response 会话
     */
    @GetMapping("verify")
    public Result<?> getCode(@RequestParam String key, HttpServletResponse response) throws IOException {
        ServletOutputStream outputStream = response.getOutputStream();
        String code = VerifyCodeUtils.generateVerifyCode(4);
        stringRedisTemplate.opsForValue().set(key, code,60, TimeUnit.SECONDS);
        VerifyCodeUtils.outputImage(150, 50, outputStream, code);
        return ResultSet.success();
    }
}
